/* This file is apart of rtodo
 * 
 * Copyright (c) 2008, Ryan Capote
 * All rights reserved
 * Released under the BSD license
 */
#include "task.h"
#include <cstdlib>
#include <iostream>
 
Task::Task(const std::string& task, int id, int priority = 0)
{
    m_task = task;
    m_priority = validatePriority(priority);
    m_project = "none";
    m_context = "none";
    m_done = false;
    m_next = false;
    m_id = id;
    //std::cout << "New 1: " << m_task << " " << m_project << " " << m_context << " " << m_priority << std::endl;
    
}

Task::Task(const std::string& task, const std::string& project,
    const std::string& context, int id, int priority = 0)
{
    //std::cout << "New 2: " << task << " " << project << " " << context << " " << priority << std::endl;
    m_task = task;
    m_project = project;
    m_context = context;
    m_priority = validatePriority(priority);
    m_done = false;
    m_next = false;
    m_id = id;
}

Task::Task(const std::string& taskdata, int id)
{
    // [done]next task (!priority @project #context)
    // [*]+  Call Joe blow (!5 @project #context)
    // [ ]  Call Joe blow (!5 @project #context)

    m_id = id;
    
    // Is the task done?
    char done = taskdata.at(1);
    m_done = (done == '*');
    
    // Is the task a next action?
    char next = taskdata.at(3);
    m_next = (next == '+');

    // We need to get the task description
    // between the 4th character and before the (
    std::string task(taskdata.substr(5, taskdata.length()));
    
    // Get the (!priority @project #context)
    std::string metadata(task.substr(task.find_first_of('('), task.length()));
    
    // Clip off the priority, project, and context
    task = task.substr(0, task.find_first_of('(') - 1);
    m_task = task;
    
    // Get the priority
    // Probably should do error checking here
    char priority = metadata.at(2);
    m_priority = validatePriority(atoi(&priority));
    
    // Cut off the priority since we don't need it
    // @project #context)
    metadata = metadata.substr(metadata.find_first_of('@'), metadata.length());
    
    // Get the project
    m_project = metadata.substr(1, metadata.find_first_of(' ') - 1);
    
    //Get the context
    m_context = metadata.substr(metadata.find_first_of('#') + 1, metadata.find_first_of(')') - 7);
    
    //std::cout << "New 3: " << m_task << " " << m_project << " " << m_context << " " << m_priority << std::endl;

}

std::string Task::serialize()
{
    std::string result;
    std::stringstream resultStream;

    //std::cout << "Serialize: " << m_task << " " << m_project << " " << m_context << " " << m_priority << std::endl;

    resultStream << "[";
    
    if(isDone())
        resultStream << "*]";
    else
        resultStream << " ]";
    
    if(isNext())
        resultStream << "+ ";
    else
        resultStream << "  ";
        
    resultStream << m_task;
    
    resultStream << " (!"
                 << m_priority
                 << " @"
                 << m_project
                 << " #"
                 << m_context
                 << ") ";
                 
    resultStream.flush();
    result = resultStream.str();
    return result;
}

std::string Task::getProject()
{
    return m_project;
}

void Task::setProject(const std::string& project)
{
    m_project = project;
}

std::string Task::getTask()
{
    return m_task;
}

void Task::setTask(const std::string& task)
{
    m_task = task;
}

std::string Task::getContext()
{
    return m_context;
}

void Task::setContext(const std::string& context)
{
    m_context = context;
}

int Task::getPriority()
{
    return m_priority;
}

void Task::setPriority(int priority)
{ 
    m_priority = validatePriority(priority);;
}

void Task::toggleNext()
{
    m_next = !m_next;
}

bool Task::isNext()
{
    return m_next;
}

void Task::toggleDone()
{
    m_done = !m_done;
}

bool Task::isDone()
{
    return m_done;
}

int Task::getId()
{
    return m_id;
}

int Task::validatePriority(int p)
{
    if(p > 5)
        p = 5;
    
    if(p < 1)
        p = 1;
        
    return p;
}


